package com.szcinda.call.service;

import com.szcinda.call.repository.Mission;
import com.szcinda.call.repository.MissionRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Transactional
public class MissionServiceImpl implements MissionService {
    private final MissionRepository missionRepository;

    public static ConcurrentHashMap<String, List<Mission>> missionMap = new ConcurrentHashMap<>();

    public MissionServiceImpl(MissionRepository missionRepository) {
        this.missionRepository = missionRepository;
    }

    @Override
    public String addMission(Mission mission) {
        String userName = mission.getUserName();
        mission.setId(UUID.randomUUID().toString());
        mission.setStatus("等待中");
        if (missionMap.containsKey(userName)) {
            List<Mission> missions = missionMap.get(userName);
            missions.add(mission);
            missionMap.put(userName, missions);
        } else {
            List<Mission> missions = new ArrayList<>();
            missions.add(mission);
            missionMap.put(userName, missions);
        }
        missionRepository.save(mission);
        return mission.getId();
    }

    @Override
    public void deleteMission(String userName, String id) {
        List<Mission> missions = missionMap.get(userName);
        if (missions == null || missions.size() == 0)
            return;
        Mission record = missions.stream().filter(mission -> mission.getId().equals(id)).findFirst().orElse(null);
        if (record == null)
            return;
        Assert.isTrue(record.getStatus().equals("等待中") || record.getStatus().equals("已完成"), "任务正在进行中,不能删除任务");
        missions.removeIf(mission -> mission.getId().equals(id));
        missionMap.put(userName, missions);
    }

    @Override
    public Mission findOneMission(String userName) {
        List<Mission> missions = missionMap.get(userName);
        if (missions == null || missions.size() == 0)
            return null;
        for (Mission mission : missions) {
            if ("等待中".equals(mission.getStatus())) {
                return mission;
            }
        }
        return null;
    }

    @Override
    public Mission findMissionById(String userName, String id) {
        List<Mission> missions = missionMap.get(userName);
        if (missions == null || missions.size() == 0)
            return null;
        for (Mission mission : missions) {
            if (id.equals(mission.getId())) {
                return mission;
            }
        }
        return null;
    }

    @Override
    public List<Mission> findMyMission(String userName) {
        return missionMap.get(userName);
    }

    @Override
    public List<Mission> findHistoryMissions(String userName) {
        return missionRepository.findByUserNameOrderByCreateTimeDesc(userName);
    }

    @Override
    public void startMission(String userName, String id) {
        List<Mission> missions = missionMap.get(userName);
        if (missions == null || missions.size() == 0)
            return;
        missions.stream().filter(mission -> mission.getId().equals(id))
                .findFirst()
                .ifPresent(mission -> mission.setStatus("进行中"));
        Mission mission = missionRepository.findFirstById(id);
        mission.setStatus("进行中");
        missionRepository.save(mission);
    }

    @Override
    public void finishMission(String userName, String id) {
        List<Mission> missions = missionMap.get(userName);
        if (missions == null || missions.size() == 0)
            return;
        missions.removeIf(mission -> mission.getId().equals(id));
        missionMap.put(userName, missions);
        Mission mission = missionRepository.findFirstById(id);
        mission.setStatus("已完成");
        missionRepository.save(mission);
    }

    @Override
    public void missionError(String userName, String id) {
        List<Mission> missions = missionMap.get(userName);
        if (missions == null || missions.size() == 0)
            return;
        missions.removeIf(mission -> mission.getId().equals(id));
        missionMap.put(userName, missions);
        Mission mission = missionRepository.findFirstById(id);
        mission.setStatus("任务失败");
        missionRepository.save(mission);
    }
}
